package com.eastmelon.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;



//压缩base

public class ImgCompress {
	/**
	 * quality 压缩质量参数
	 * imgStr  图片Base64编码 
	 */
	public static String resizeImage(int width,int height,float quality,String imgStr,String imgType) throws IOException {
		String newImgStr="";
		try {
			BufferedImage bg=null;
			BufferedImage sourceImage=Base64Util.readMemoryImage(imgStr);
			bg=Thumbnails.of(sourceImage).size(width, height)
					.keepAspectRatio(false).outputQuality(quality).asBufferedImage();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(bg, imgType, out);
			byte [] b=out.toByteArray();
			newImgStr=Base64.encode(b);
		} catch (IllegalStateException e) {
			newImgStr="";
			System.out.println("压缩失败:"+e);
		}
		return newImgStr;
	}
}
